//
//  FKSearchBar.m
//  UISeachBar
//
//  Created by 冯凯 on 14-12-2.
//  Copyright (c) 2014年 冯凯. All rights reserved.
//

#import "FKSearchBar.h"

@implementation FKSearchBar
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup
{
    self.searchBar.backgroundColor = [UIColor colorWithRed:237.0/255 green:237.0/255 blue:237.0/255 alpha:1];
    self.textfiledView = [[UIView alloc]initWithFrame:CGRectMake(15, 6, kScreenWidth - 30, 32)];
    self.textfiledView.layer.masksToBounds = YES;
    self.textfiledView.layer.cornerRadius = 5;
    self.textfiledView.backgroundColor = [UIColor whiteColor];
    UIImageView * searchImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 6, 18, 18)];
    searchImageView.image = [UIImage imageNamed:@"公用_搜索@2x"];
    self.searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(35, -5, kScreenWidth - 60 - 18 -10, 40)];
    self.searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.searchTextField.returnKeyType = UIReturnKeyDone;
    self.searchTextField.font = [UIFont systemFontOfSize:15];
    self.searchTextField.delegate = self;
    self.searchTextField.textColor = [UIColor colorWithRed:31.0/255 green:33.0/255 blue:36.0/255 alpha:1];
    self.searchTextField.borderStyle= UITextBorderStyleNone;
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth - 30 - 18- 10, 2, 70, 40)];
    imageView.userInteractionEnabled = YES;
    UIImageView * rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth - 30 - 18 - 10, 6, 18, 18)];
    rightImageView.userInteractionEnabled = YES;
    rightImageView.image = [UIImage imageNamed:@"公用_语音@2x"];
    UITapGestureRecognizer * rightTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(rightTap)];
    UIImageView * lineView = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.height - 1, kScreenWidth, 1)];
    lineView.backgroundColor = [UIColor colorWithRed:221.0/255 green:221.0/255 blue:221.0/255 alpha:1];
    [imageView addGestureRecognizer:rightTap];
    [self.textfiledView addSubview:rightImageView];
    [self.textfiledView addSubview:self.searchTextField];
    [self.textfiledView addSubview:searchImageView];
    [self.searchBar addSubview:self.textfiledView];
    [self addSubview:self.searchBar];
    [self addSubview:imageView];
    [self addSubview:lineView];
    
[self.searchTextField addTarget:self action:@selector(textFieldTextDidChangeOneCI:) forControlEvents:UIControlEventEditingChanged];
}

-(void)textFieldTextDidChangeOneCI:(UITextField*)tf{
    
    
}

- (UIView *)searchBar
{
    if (_searchBar == nil) {
        _searchBar = [[UIView alloc] init];
        _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    }
    return _searchBar;
}
#pragma mark - Layout
- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    CGRect aFrame = CGRectZero;
    aFrame.origin = self.bounds.origin;
    aFrame.size = frame.size;
    self.searchBar.frame = aFrame;
}
- (void)rightTap
{
    if ([self.fkSearchRequestDelegate respondsToSelector:@selector(VoiceConversion)]) {
        [self.fkSearchRequestDelegate VoiceConversion];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([self.fkSearchRequestDelegate respondsToSelector:@selector(SearchString)]) {
        [self.fkSearchRequestDelegate SearchString];
    }
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([textField.text length] != 0){
    if ([self.fkSearchRequestDelegate respondsToSelector:@selector(SearchStringWithkeyword)]) {
        [self.fkSearchRequestDelegate SearchStringWithkeyword];
    }
    }
    return YES;
}
// return NO to not change text
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



@end
